package com.swt.draw.controller;

import com.swt.draw.dao.*;
import com.swt.draw.entity.*;
import com.swt.draw.example.*;
import com.swt.draw.service.PostService;
import com.swt.draw.service.RoleService;
import com.swt.draw.service.TemplateService;
import com.swt.draw.vo.AdminDetailVO;
import com.swt.draw.vo.TemplateVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @ClassName
 * @Description TODO
 * @Author Administrator
 * @Date 2019/8/20 0020 0:36
 * @Version 1.0
 **/
@Controller
public class VeiwController {
    @Resource
    private AdminMapper adminMapper;
    @Resource
    private PostMapper postMapper;
    @Resource
    private PostService postService;
    @Resource
    private RoleService roleService;
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private LimitMapper limitMapper;
    @Resource
    private RoleLimitMapper roleLimitMapper;
    @Resource
    private TemplateService templateService;
    /**
     * @Author ZPF
     * @Description 登录页
     * @Date 22:03 2019/8/22 0022
     * @Param []
     * @return java.lang.String
     **/
    @RequestMapping("/login")
    public String login(){
        return "login";
    }

    /**
     * @Author ZPF
     * @Description 主页
     * @Date 22:02 2019/8/22 0022
     * @Param [model]
     * @return java.lang.String
     **/
    @RequestMapping(value = {"/index","/"})
    public String index(Model model){
        Admin admin = getAdminDetail();
        model.addAttribute("admin", admin);
        return "index";
    }

    /**
     * @Author ZPF
     * @Description 个人中心
     * @Date 22:02 2019/8/22 0022
     * @Param []
     * @return java.lang.String
     **/
    @RequestMapping("/userCenter")
    public String userCenter(Model model){
        Admin admin = getAdminDetail();
        model.addAttribute("admin", admin);
        return "userCenter";
    }

    /**
     * @Author ZPF
     * @Description 用户列表
     * @Date 22:02 2019/8/22 0022
     * @Param []
     * @return java.lang.String
     **/
    @RequestMapping("/userList")
    public String userList(){
        return "userList";
    }

    /**
     * @Author ZPF
     * @Description 账号详情页
     * @Date 22:08 2019/8/22 0022
     * @Param []
     * @return java.lang.String
     **/
    @RequestMapping("/myselfInfo")
    public String userDetailInfo(Model model){
        Admin admin = (Admin) SecurityUtils.getSubject().getPrincipal();
        AdminDetailVO detail = adminMapper.getAdminDetailInfoByNo(admin.getAdminNo());
        model.addAttribute("admin", detail);
        return "myselfInfo";
    }

    /**
     * @Author ZPF
     * @Description 角色管理列表
     * @Date 16:50 2019/8/31 0031
     * @Param [model]
     * @return java.lang.String
     **/
    @RequestMapping("/roleList")
    public String roleList(Model model){
        LimitExample example = new LimitExample();
        LimitExample.Criteria criteria = example.createCriteria();
        criteria.andLimitStatusNotEqualTo((short) 2);
        List<Limit> limits = limitMapper.selectByExample(example);
        model.addAttribute("limits", limits);
        return "roleList";
    }

    /**
     * @Author ZPF
     * @Description 角色编辑
     * @Date 13:32 2019/9/1 0001
     * @Param [model, roleNo]
     * @return java.lang.String
     **/
    @RequestMapping("/roleEdit")
    public String roleEdit(Model model, String roleNo){
        RoleExample roleExample = new RoleExample();
        RoleExample.Criteria roleCriteria = roleExample.createCriteria();
        roleCriteria.andRoleNoEqualTo(roleNo);
        List<Role> roles = roleMapper.selectByExample(roleExample);
        model.addAttribute("role", roles.get(0));
        LimitExample example = new LimitExample();
        LimitExample.Criteria criteria = example.createCriteria();
        criteria.andLimitStatusNotEqualTo((short) 2);
        List<Limit> limits = limitMapper.selectByExample(example);
        model.addAttribute("limits", limits);
        // 获取该角色拥有的权限
        RoleLimitExample roleLimitExample = new RoleLimitExample();
        RoleLimitExample.Criteria criteria1 = roleLimitExample.createCriteria();
        criteria1.andRoleLimitRoleNoEqualTo(roleNo);
        List<RoleLimit> roleLimits = roleLimitMapper.selectByExample(roleLimitExample);
        model.addAttribute("roleLimit", roleLimits);
        return "roleEdit";
    }
    /**
     * @Author ZPF
     * @Description 编辑用户信息
     * @Date 11:06 2019/8/24 0024
     * @Param []
     * @return java.lang.String
     **/
    @RequestMapping("/userEdit")
    public String editInfo(String adminNo, Model model){
        AdminDetailVO adminDetail = null;
        if(StringUtils.isEmpty(adminNo)){
            Admin admin = (Admin) SecurityUtils.getSubject().getPrincipal();
            adminDetail = adminMapper.getAdminDetailInfoByNo(admin.getAdminNo());
        }else {
            adminDetail = adminMapper.getAdminDetailInfoByNo(adminNo);
        }
        List<Post> posts = postService.getAllPosts();
        List<Role> roles = roleService.getAllRoles();
        model.addAttribute("admin", adminDetail);
        model.addAttribute("posts", posts);
        model.addAttribute("roles",roles);
        return "userEdit";
    }

    /**
     * @Author ZPF
     * @Description 添加账号
     * @Date 14:08 2019/9/1 0001
     * @Param [model]
     * @return java.lang.String
     **/
    @RequestMapping("/userAdd")
    public String addUser(Model model){
        // 查询岗位列表
        PostExample example = new PostExample();
        PostExample.Criteria criteria = example.createCriteria();
        criteria.andPostStatusEqualTo((short) 0);
        List<Post> posts = postMapper.selectByExample(example);
        List<Role> roles = roleService.getAllRoles();
        model.addAttribute("roles",roles);
        model.addAttribute("postList", posts);
        return "userAdd";
    }

    /**
     * @Author ZPF
     * @Description 权限列表
     * @Date 14:09 2019/9/1 0001
     * @Param []
     * @return java.lang.String
     **/
    @RequestMapping("/limitList")
    public String limitList(){
        return "limitList";
    }

    /**
     * @Author ZPF
     * @Description 岗位列表
     * @Date 15:58 2019/9/1 0001
     * @Param []
     * @return java.lang.String
     **/
    @RequestMapping("/postList")
    public String postList(){
        return "postList";
    }

    /**
     * @Author ZPF
     * @Description 编辑岗位
     * @Date 16:27 2019/9/1 0001
     * @Param []
     * @return java.lang.String
     **/
    @RequestMapping("/postEdit")
    public String postEdit(Model model, String postNo){
        PostExample example = new PostExample();
        PostExample.Criteria criteria = example.createCriteria();
        criteria.andPostNoEqualTo(postNo);
        List<Post> posts = postMapper.selectByExample(example);
        model.addAttribute("post", posts.get(0));
        return "postEdit";
    }

    /**
     * @Author ZPF
     * @Description 机构列表
     * @Date 16:57 2019/9/1 0001
     * @Param [model]
     * @return java.lang.String
     **/
    @RequestMapping("/departmentList")
    public String departmentList(Model model){
        return "departmentList";
    }

    /**
     * @Author ZPF
     * @Description 工具绘图
     * @Date 21:11 2019/9/2 0002
     * @Param []
     * @return java.lang.String
     **/
    @RequestMapping("/drawTool")
    public String tool(){
        return "tool";
    }

    /**
     * @Author ZPF
     * @Description 模板管理
     * @Date 22:03 2019/10/19 0019
     * @Param []
     * @return java.lang.String
     **/
    @RequestMapping("/templateManager")
    public String templateManager(Model model){
        // 获取当前用户模板总数
        Admin admin = (Admin) SecurityUtils.getSubject().getPrincipal();
        Integer count = templateService.getYjTemplateCount(admin.getAdminNo());
        model.addAttribute("count", count);
        return "templateManager";
    }

    @RequestMapping("/history")
    public String history(Model model) {
        return "history";
    }

    /**
     * @Author ZPF
     * @Description 模板编辑
     * @Date 0:40 2019/10/28 0028
     * @Param [model]
     * @return java.lang.String
     **/
    @RequestMapping("/templateEdit")
    public String templateEdit(String jh, String time, Model model){
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSZ");
        return "templateEdit";
    }

    /**
     * @Author ZPF
     * @Description 工具管理
     * @Date 23:05 2019/10/20 0020
     * @Param []
     * @return java.lang.String
     **/
    @RequestMapping("/toolManager")
    public String toolManager(){
        return "toolManager";
    }

    private Admin getAdminDetail(){
        Admin admin = (Admin) SecurityUtils.getSubject().getPrincipal();
        AdminExample example = new AdminExample();
        AdminExample.Criteria criteria = example.createCriteria();
        criteria.andAdminNoEqualTo(admin.getAdminNo());
        List<Admin> admins = adminMapper.selectByExample(example);
        return admins.get(0);
    }
}
